<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/3
 * Time: 13:59
 */

namespace common\models\Service;


use common\models\Cache;
use common\models\CommonFunction;
use common\models\DAO\IntegralSetting;
use common\models\DAO\User;
use common\models\DAO\UserIntegral;
use common\models\DAO\UserIntegralLog;

class IntegralService {

    public static function get($type, $day){
        $cache_name = 'integral_'.$type;
        $cache = Cache::get($cache_name);
        if(!$cache){
            $cache = $inteal = IntegralSetting::find()->where(['type'=>$type])->orderBy('day desc')->asArray()->all();
            Cache::set($cache_name, $cache);
        }
        $scale = $cache[count($cache) - 1]['scale'];
        foreach($cache as $v){
            if($day >= $v['day']){
                $scale = intval($v['scale']);
            }
        }
        return intval($scale)/10;
    }


    /*
     * 投资返积分
     */
    public static function add($param){
        $day = $param['day'];
        $money = $param['money'];
        $investID = 0;
        $userInvestID = $param['userInvestID'];
        $userID = $param['userID'];
        $createTime = $param['createTime'];
        $desc = $param['desc'];
        $productID = 0;
        isset($param['productID']) && $productID = $param['productID'];
        isset($param['investID']) && $investID = $param['investID'];
        $scale = self::get(1, $day);
        $integral = intval($money / 100 / $scale);
        if($integral <= 0)
            return true;
        $user_integral = new UserIntegral();
        $user_integral->investID = $investID;
        $user_integral->productID = $productID;
        $user_integral->userInvestID = $userInvestID;
        $user_integral->userID = $userID;
        $user_integral->createTime = $createTime;
        $user_integral->updateTime = $createTime;
        $user_integral->integral = $integral;
        $user_integral->usable = $integral;
        $user_integral->type = 1;
        $user_integral_result = $user_integral->save();
        if(!$user_integral_result)
            return false;
        $user_integral_log = new UserIntegralLog();
        $user_integral_log->userID = $userID;
        $user_integral_log->investID = $investID;
        $user_integral_log->productID = $productID;
        $user_integral_log->userInvestID = $userInvestID;
        $user_integral_log->createTime = $createTime;
        $user_integral_log->integral = $integral;
        $user_integral_log->des = $desc;
        $user_integral_log->type = 1;
        $user_integral_log->road = 1;
        $user_integral_log_result = $user_integral_log->save();
        if(!$user_integral_log_result)
            return false;
        return true;
    }

    /*
     * 用户获得积分
     */
    public static function info($userID, $type = 1, $page = 1, $limit = 10){
        if($page <= 0)
            $page = 1;
        if($limit <= 0)
            $limit = 10;
        $start = (intval($page) - 1) * $limit;
        $list = UserIntegralLog::find()->where(['userID'=>$userID, 'type'=>$type])->asArray()->limit($limit)
            ->offset($start)->orderBy('id desc')->all();
        $result = [];
        foreach($list as $k=>$v){
            //类型
            if($type==1 or $type==3)
            {
                if($v['road']==1)
                {
                    $data['road']='投资';
                }else
                {
                    $data['road']='好友投资';
                }
            }else
            {
                if($v['road']==1)
                {
                    $data['road']='兑换';
                }else
                {
                    $data['road']='抽奖';
                }
            }
            $data['road'] = $v['des'];
            $data['desc'] = $v['des'];
            $data['time'] = date('Y-m-d H:i:s', $v['createTime']);
            $data['webtime'] = date('Y-m-d', $v['createTime']);
            $data['integral'] = $v['integral'];
            $result[] = $data;
        }
        return $result;
    }

    /*
     * 积分统计
     */
    public static function total($userID, $type = 1){
        $integral = 0;
        $model = UserIntegralLog::find()->where(['userID'=>$userID, 'type'=>$type])->sum('integral');
        if($model)
            $integral = $model;
        return $integral;

    }

    //用户增加积分
    public static function addIntegral($param){
        $investID = 0;
        $userInvestID = 0;
        $productID = 0;
        $type = 3;
        $userID = 0;
        if(isset($param['investID']))
            $investID = $param['investID'];
        if(isset($param['userInvestID']))
            $userInvestID = $param['userInvestID'];
        if(isset($param['productID']))
            $productID = $param['productID'];
        if(isset($param['type']))
            $type = $param['type'];
        if(isset($param['userID']))
            $userID = $param['userID'];
        $rec_info = User::findOne($param['recID']);
        if(!$rec_info) return false;
        $now = time();
        /*if($type == 3 || $type == 4){
            $openID = '';
            $rec_info['openID'] && $openID = $rec_info['openID'];
            $brokerage_param = ['userID'=>$param['recID'], 'investUserID'=>$userID, 'createTime'=>$now, 'openID'=>$openID];
            $brokerage_money = 30;
            $brokerage_desc = '您的好友打开您分享的链接';
            $brokerage_typeID = 11;
            if($type == 4){
                $brokerage_money = 50;
                $brokerage_typeID = 12;
                $brokerage_desc = "您的好友".CommonFunction::dealTel($param['tel'])."关注微信公众平台";
            }
            $brokerage_param['money'] = $brokerage_money;
            $brokerage_param['desc'] = $brokerage_desc;
            $brokerage_param['typeID'] = $brokerage_typeID;
            DayService::brokerage($brokerage_param);
        }*/
        $user_integral = new UserIntegral();
        $user_integral->investID = $investID;
        $user_integral->userInvestID = $userInvestID;
        $user_integral->investUserID = $userID;
        $user_integral->userID = $param['recID'];
        $user_integral->createTime = $now;
        $user_integral->updateTime = $now;
        $user_integral->integral = $param['integral'];
        $user_integral->usable = $param['integral'];
        $user_integral->productID = $productID;
        $user_integral->type = $type;
        $user_integral_result = $user_integral->save();
        if(!$user_integral_result)
            return false;
        $user_integral_log = new UserIntegralLog();
        $user_integral_log->userID = $param['recID'];
        $user_integral_log->investID = $investID;
        $user_integral_log->userInvestID = $userInvestID;
        $user_integral_log->createTime = $now;
        $user_integral_log->integral = $param['integral'];
        $user_integral_log->productID = $productID;
        $user_integral_log->type = 1;
        $user_integral_log->road = $type;
        $desc = $param['des'];
        if($type == 8){
            $desc = "系统补偿。";
        }
        $user_integral_log->des = $desc;
        $user_integral_log_result = $user_integral_log->save();
        if($user_integral_log_result){
            if(!empty($rec_info['openID'])){
                $title = "您获得了{$param['integral']}个积分";

                $content = "您的好友通过打开您分享的内容，加入您的财团。";

                if($type == 4){
                    $content = "您的好友".CommonFunction::dealTel($param['tel'])."关注微信公众平台，您获得了{$param['integral']}个积分";
                }
                if($type == 5){
                    $content = "您的好友".CommonFunction::dealTel($param['tel'])."完成新手任务，您获得了{$param['integral']}个积分";
                }
                if($type == 6){
                    $content = "今日签到，您获得了{$param['integral']}个积分";
                }
                if($type == 7){
                    $content = "您的好友通过扫描您的二维码，加入您的财团。";
                }
                if($type == 8 || $type == 9){
                    $content = $param['des'];
                }
                SendWxMessage::integral($rec_info['openID'], $param['integral'], $title, $content);
            }
            return true;
        }else
            return false;

    }

    /*
     * 用户剩余积分
     */
    public static function remainder($userID){
        $interal = 0;
        $result = UserIntegral::find()->where(['userID'=>$userID])->andWhere('usable > 0')->sum('usable');
        $result && $interal = $result;
        return $interal;
    }

    /*
     * 积分条目查询
     */
    public static function init($userID){
        //未使用总条目数
        $data['wei']=UserIntegralLog::find()->where(['userID'=>$userID, 'type'=>1])->count();
        //未使用总积分数
        $data['Inwei']=UserIntegralLog::find()->where(['userID'=>$userID, 'type'=>1])->sum('integral');
        //已使用总条目数
        $data['yi']=UserIntegralLog::find()->where(['userID'=>$userID, 'type'=>2])->count();
        //已使用总积分数
        $data['Inyi']=UserIntegralLog::find()->where(['userID'=>$userID, 'type'=>2])->sum('integral');
        //已过期总条目数
        $data['guoqi']=UserIntegralLog::find()->where(['userID'=>$userID, 'type'=>3])->count();
        return $data;
    }




}
